<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="产品编码:">
              <el-input
                clearable
                v-model="where.invcode"
                placeholder="输入物料编码或物料名称查询"/>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="制单人:">
              <el-input
                clearable
                v-model="where.createUserName"
                placeholder=""/>
            </el-form-item>
          </el-col>

          <el-col :md="5" :sm="12">
            <el-form-item label="起始日期:">

              <el-date-picker v-model="where.begindate" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :md="5" :sm="12">
            <el-form-item label="结束日期:">
              <el-date-picker v-model="where.enddate" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <div class="ele-form-actions" style="padding-top: 3px">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="mini"
                @click="reload">查询
              </el-button>
              <el-button @click="reset" size="mini">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <el-row :gutter="15" style="margin-top: 5px;">
        <el-col :lg="12" style="margin-bottom: 15px;">
          <ele-pro-table
            ref="table"
            :where="where"
            :page-size="20"
            :datasource="url"
            :columns="columns"
            :selection.sync="selection"
            :loading="tableLoading"
            @row-click="rowClick"
            @row-dblclick="openDetail"
            height="calc(100vh - 250px)">
            <!-- 表头工具栏 -->
            <template slot="toolbar">
              <el-button
                size="small"
                type="primary"
                icon="el-icon-plus"
                class="ele-btn-icon"
                @click="openEdit(null)"
                v-if="permission.includes('sys:spbcinvset:add')">添加
              </el-button>
<!--              <el-button-->
<!--                size="small"-->
<!--                type="primary"-->
<!--                icon="el-icon-check"-->
<!--                class="ele-btn-icon"-->
<!--                @click="approve"-->
<!--                v-if="permission.includes('sys:spbcinvset:approve')">审批-->
<!--              </el-button>-->
<!--              <el-button-->
<!--                size="small"-->
<!--                type="warning"-->
<!--                icon="el-icon-close"-->
<!--                class="ele-btn-icon"-->
<!--                @click="reject"-->
<!--                v-if="permission.includes('sys:spbcinvset:approve')">驳回-->
<!--              </el-button>-->
<!--              <el-button-->
<!--                size="small"-->
<!--                type="primary"-->
<!--                class="ele-btn-icon"-->
<!--                icon="el-icon-close"-->
<!--                @click="cancelApprove"-->
<!--                v-if="permission.includes('sys:spbcinvset:approve')">取消审批-->
<!--              </el-button>-->
              <el-button
                size="small"
                type="danger"
                icon="el-icon-delete"
                class="ele-btn-icon"
                @click="removeBatch"
                v-if="permission.includes('sys:spbcinvset:delete')">删除
              </el-button>
            </template>
            <template slot="bcgzStatus" slot-scope="{row}">
              <el-tag
                :type="{'1':'primary', '2':'success', '-1':'warning'}[row.mark.toString()]"
                size="mini">
                {{ {'1':'待审批', '2':'已审批','-1':'已驳回'}[row.mark.toString()] }}
              </el-tag>
            </template>
            <!-- 操作列 -->
            <template slot="action" v-if="true" slot-scope="{row}">
              <el-link
                type="primary"
                :underline="false"
                icon="el-icon-edit"
                @click="openEdit(row)"
                v-if="permission.includes('sys:spbcinvset:edit') && row.mark!=2">修改
              </el-link>
              <el-link
                type="primary"
                :underline="false"
                icon="el-icon-document-copy"
                @click="copy(row)"
                v-if="permission.includes('sys:spbcinvset:edit')">复制
              </el-link>
            </template>
          </ele-pro-table>
        </el-col>
        <el-col :lg="12" style="margin-bottom: 15px;">
          <ele-pro-table
            ref="tableDetail"
            :page-size="20"
            :where="whereDetail"
            :datasource="urlDetail"
            :columns="columnsDetail"
            :selection.sync="selectionDetail"
            :loading="tableDetailLoading"
            :toolkit="['reload', 'columns', 'fullscreen']"
            height="calc(100vh - 250px)">
            <!-- 表头工具栏 -->
            <template slot="toolbar">
              <el-button
                size="small"
                type="primary"
                icon="el-icon-plus"
                class="ele-btn-icon"
                @click="openSelect()"
                v-if="permission.includes('sys:spbcinvset:add')">添加
              </el-button>
<!--              <el-button-->
<!--                size="small"-->
<!--                type="primary"-->
<!--                icon="el-icon-edit"-->
<!--                class="ele-btn-icon"-->
<!--                @click="openNowSalePriceEdit()"-->
<!--                v-if="permission.includes('sys:spbcinvset:add')">录入医院价格-->
<!--              </el-button>-->
              <el-button
                size="small"
                type="danger"
                icon="el-icon-delete"
                class="ele-btn-icon"
                @click="removeDetailBatch"
                v-if="permission.includes('sys:spbcinvset:delete')">删除
              </el-button>
<!--              <el-button-->
<!--                size="small"-->
<!--                type="success"-->
<!--                icon="el-icon-upload"-->
<!--                class="ele-btn-icon"-->
<!--                @click="uploadAttach"-->
<!--                v-if="permission.includes('sys:spbcinvset:add')">上传价格依据-->
<!--              </el-button>-->
            </template>
          </ele-pro-table>
        </el-col>
      </el-row>
    </el-card>
    <AddPrice :visible.sync="showEdit" :data="current" @done="saveinv"></AddPrice>
    <AddDetail :visible.sync="showSelect" :data="current" @done="reloadDetail"></AddDetail>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import AddPrice from './frmAddPrice'
import AddDetail from './frmAddDetail'
export default {
  name: 'ExamSpbcInvset',
  components:{AddPrice,AddDetail},
  data() {
    return {
      // 表格数据接口
      url: '/invprice/index',
      urlDetail:'/invprice/detail',
      tableLoading:false,
      tableDetailLoading:false,
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          columnKey: 'index',
          type: 'index',
          label: '序号',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'invcode',
          label: '产品编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'invname',
          label: '产品名称',
          showOverflowTooltip: true,
          minWidth: 70,
          align: 'center'
        },
        {
          prop: 'createTime',
          label: '最后更新时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 120,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      columnsDetail: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          columnKey: 'index',
          type: 'index',
          label: '序号',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'stockQty',
          label: '库存数量',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'left'
        },
        {
          prop: 'billtime',
          label: '制单时间',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'left'
        },
        {
          prop: 'controlBeginTime',
          label: '控价开始时间',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'left'
        },
        {
          prop: 'controlEndTime',
          label: '控价结束时间',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'left'
        },
        {
          prop: 'convertRule',
          label: 'NC转换规则',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'convertRate',
          label: 'NC转换系数',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'left'
        },
        {
          prop: 'controlPrice',
          label: '控价价格',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'right'
        },
        {
          prop: 'convertedPrice',
          label: '转化后价格',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'syncTime',
          label: '同步日期',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'left'
        },
        {
          prop: 'syncStatus',
          label: '同步状态',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'left'
        },
        {
          prop: 'createTime',
          label: '最后更新时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
      ],
      // 表格搜索条件
      where: {},
      whereDetail: {
        pid: 0
      },
      // 表格选中数据
      selection: [],
      selectionDetail: [],
      pid:Number,
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showSelect: false,
      showBalanceAmount: false,
      showNowSalePrice: false,
      showDetail: false
    };
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  mounted() {

  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.tableLoading=true;
      this.tableDetailLoading=true;
      this.$refs.table.reload({where: this.where});
      this.$refs.tableDetail.reload({where: this.whereDetail});
      this.tableLoading=false;
      this.tableDetailLoading=false;
    },
    reloadDetail(){
      this.tableDetailLoading=true;
      this.$refs.tableDetail.reload({where: this.whereDetail});
      this.tableDetailLoading=false;
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.whereDetail ={pid: 0};
      this.reload();
    },
    rowClick(row)
    {
      this.tableDetailLoading=true;
      this.$refs.table.clearSelection();
      this.$refs.table.toggleRowSelection(row,true);
      this.whereDetail.pid = row.id;
      this.$refs.tableDetail.reload({where: this.whereDetail});
      this.tableDetailLoading=false;

    },
    saveinv(code,name){
      let param ={
        invcode:code,
        invname:name
      };
      this.$http.post('/invprice/add',param).then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示编辑 */
    openSelect() {

      if(this.selection.length==0)
      {
        this.$message.info("请选择一个补差依据后添加药品编码");
        return;
      }
      this.current = this.selection[0];

        this.pid = Number(this.selection[0].id);
        this.showSelect = true;
    },
    openBalanceAmoutEdit() {
      if(this.selection.length==0)
      {
        this.$message.info("请选择一个补差依据");
        return;
      }
      this.pid = Number(this.selection[0].id);
      this.showBalanceAmount = true;
    },
    openNowSalePriceEdit() {
      if(this.selection.length==0)
      {
        this.$message.info("请选择一个补差依据");
        return;
      }
      if(this.selection[0].mark==2){
        this.$message.error('已审批规则不能修改');
        return;
      }else{
        this.pid = Number(this.selection[0].id);
        this.showNowSalePrice = true;
      }
    },

    saveInv()
    {
      //console.log(invcodes);
      this.whereDetail.pid = this.pid;
      this.$refs.tableDetail.reload({where: this.whereDetail});
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/spbcinvset/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      for(let s of this.selection){
        if(s.mark==2){
          this.$message.error('已审批规则不能删除');
          return;
        }
      }
      this.$confirm('确定要删除选中的吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/invprice/delete/'+this.selection.map(d => d.id)).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    removeDetailBatch(){
      if (!this.selectionDetail.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      if(this.selection[0].mark==2){
        this.$message.error('已审批规则不能修改');
        return;
      }else{
        this.$confirm('确定要删除选中的吗?', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.delete('/invpricedetail/delete/'+this.selectionDetail.map(d => d.id)).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reloadDetail();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => {
        });
      }
    },
    openDetail(row){
      this.current = row;
      this.showDetail = true;
    },
    approve(){
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      for(let s of this.selection){
        if(s.mark == 1 || s.mark == -1){
          let mark = 2;
          this.$http.post('/spbcinvset/updateMark/'+s.id+'/'+mark).then(res => {
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.$message.error(e.message);
          });
        }
      }
    },
    reject(){
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      for(let s of this.selection){
        if(s.mark == 1){
          let mark = -1;
          this.$http.post('/spbcinvset/updateMark/'+s.id+'/'+mark).then(res => {
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.$message.error(e.message);
          });
        }
      }
    },
    cancelApprove(){
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      for(let s of this.selection){
        if(s.mark == 2){
          let mark = 1;
          this.$http.post('/spbcinvset/updateMark/'+s.id+'/'+mark).then(res => {
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.$message.error(e.message);
          });
        }
      }
    },
    copy(row){
      this.$confirm('确定要复制该补差规则吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.post('/spbcinvset/copy/'+row.id).then(res => {
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    }
  }
}
</script>

<style scoped>
</style>
